#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2025 Huawei Technologies Co., Ltd. All rights reserved.

from ansible.module_utils.basic import AnsibleModule

from openubmc_ansible.openubmc_redfish_api.redfish_base import BmcBaseConnect
from openubmc_ansible.openubmc_redfish_api.api_manage_bios import reset_bios
from openubmc_ansible.openubmc_logger import log, report
from openubmc_ansible.utils import ansible_bmc_run_module, SERVERTYPE, is_support_server


ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}

DOCUMENTATION = """
---
module: openubmc_reset_bios
short_description: Reset BIOS resource attributes
version_added: "2.5.0"
description:
    - "Reset BIOS resource attributes"
options:
  openubmc_ip:
    required: true
    default: None
    description:
      - openUBMC IP address
  openubmc_user:
    required: true
    default: None
    description:
      - openUBMC user name used for authentication
  openubmc_pswd:
    required: true
    default: None
    description:
      - openUBMC user password used for authentication
  Immediately:
    required: false
    default: False
    description:
      - Whether to restart the system immediately for the configuration to take effect.
"""

EXAMPLES = """
 - name: reset openubmc bios
    openubmc_reset_bios :
      openubmc_ip: "{{ openubmc_ip }}"
      openubmc_user: "{{ openubmc_user }}"
      openubmc_pswd: "{{ openubmc_pswd }}"
      Immediately: False
"""

RETURNS = """
    {"result": True, "msg": "Reset BIOS configuration resource info successfully!"}
"""


def openubmc_reset_bios_module(module):
    """
    Function:
        Reset BIOS resource attributes
    Args:
        module : information from yml
    Returns:
        ret : Task result
            "result": True or False
            "msg": description for success or failure
    Raises:
        None
    Date: 2021/2/22
    """
    with BmcBaseConnect(module.params, log, report) as openubmc:
        ret = is_support_server(openubmc, SERVERTYPE)
        if ret['result']:
            ret = reset_bios(openubmc, module.params["Immediately"])
    return ret


def main():
    # Use AnsibleModule to read yml files and convert it to dict
    module = AnsibleModule(
        argument_spec={
            "openubmc_ip": {"required": True, "type": 'str'},
            "openubmc_user": {"required": True, "type": 'str'},
            "openubmc_pswd": {"required": True, "type": 'str', "no_log": True},
            "Immediately": {"required": False, "type": 'bool'}
        },
        supports_check_mode=False)

    ansible_bmc_run_module(openubmc_reset_bios_module, module, log, report)


if __name__ == '__main__':
    main()
